package com.brillilab.service.logic.lab;

import com.brillilab.common.utils.DateUtil;
import com.brillilab.domain.enums.lab.InviteCodeTypeEnum;
import com.brillilab.domain.enums.lab.LabMemberEnum;
import com.brillilab.domain.enums.lab.LabPayTypeEnum;
import com.brillilab.domain.enums.lab.UserPayTypeEnum;
import com.brillilab.domain.po.lab.Lab;
import com.brillilab.domain.po.lab.LabExpireJob;
import com.brillilab.domain.po.lab.LabMember;
import com.brillilab.domain.po.user.InviteCode;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.lab.ILabExpireJobService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.lab.ILabService;
import com.brillilab.service.core.user.IInviteCodeService;
import com.brillilab.service.logic.message.MessageLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.service.module.aliyun.AliYunSms;
import com.brillilab.service.quartz.BrillQuartzScheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 实验室业务逻辑
 */
@Service
@Transactional
public class LabLogic {

    @Resource
    private ILabService labService;
    @Resource
    private ILabMemberService labMemberService;
    @Resource
    private BrillQuartzScheduler scheduler;
    @Resource
    private ILabExpireJobService labExpireJobService;
    @Resource
    private IInviteCodeService inviteCodeService;
    @Resource
    private MessageLogic messageLogic;
    @Resource
    private AliYunSms aliYunSms;
    @Resource
    private UsersLogic usersLogic;

    /**
     * PI分享成功升级为免费版
     *
     * @param user
     * @return
     */
    public List<String> shareSuccess(UserInfoVo user) {
        Assert.isTrue(!user.getUserPayType().equals(UserPayTypeEnum.FREE.getValue()), "您的账号已升级为正式账号，无需重复进行!");
        //用户账号过期时间延长
        usersLogic.delayUserExpireDate(user);
        Lab lab = labService.selectById(user.getLabId());
        if (lab != null) {
            if (user.getIsOwner()) {
                //PI分享
                lab.setPayType(LabPayTypeEnum.FREE.getValue());
                //100years
                lab.setExpireDate(DateUtil.addYear(new Date(), 100));
                labService.updateById(lab);
                //删除试用实验室过期定时任务
                this.deleteLabExpireJobs(lab.getId());

                List<String> inviteCodes = new ArrayList<>();
                List<InviteCode> inviteCodeInfo = inviteCodeService.getInviteCodeInfo(lab.getId());
                if (CollectionUtils.isEmpty(inviteCodeInfo)) {
                    //生成邀请码
                    inviteCodes = inviteCodeService.generateInviteCodes(InviteCodeTypeEnum.USER_INVITE.getName(), 3, user);
                    //发送短信邀请码给PI
                    Map<String, Object> paraMap = new HashMap<>();
                    paraMap.put("userName", user.getName());
                    paraMap.put("labName", user.getLabName());
                    messageLogic.sendSmsContent(user.getMobile(), aliYunSms.getShareLabSuccessPI(), paraMap);
                    //刷新缓存提示
                    List<LabMember> labMembers = labMemberService.selectPoList(user.getLabId(), LabMemberEnum.State.STATE_IN.getValue());
                    for (LabMember labMember : labMembers) {
                        usersLogic.setRefreshUser(labMember.getId(), "您的实验室已升级，请刷新用户信息!", false);
                    }
                }
                return inviteCodes;
            }
        }
        return null;
    }

    /**
     * 试用实验室过期定时任务删除
     *
     * @param labId
     * @return
     */
    public void deleteLabExpireJobs(Long labId) {
        List<LabExpireJob> labExpireJobs = labExpireJobService.selectByLabId(labId);
        labExpireJobs.forEach(e -> {
            scheduler.deleteJob(e.getJobName(), e.getJobGroup());
        });
        labExpireJobService.deleteByLabId(labId);
    }

}
